package com.nowoe.data.sqlSession;

import com.nowoe.data.config.Configuration;
import com.nowoe.data.constant.SqlCommandTypeEnum;
import com.nowoe.data.exector.Executor;
import com.nowoe.data.statement.MapperStatement;

import java.lang.reflect.*;
import java.util.List;

/**
 * @author 倚栏听风 2023/9/14 16:44
 */
public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;
    private Executor executor;

    public DefaultSqlSession(Configuration configuration, Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
    }

    @Override
    public <E> List<E> selectList(String statementId, Object param) throws Exception {
        //执行jdbc底层query
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        return executor.query(configuration, mapperStatement, param);
    }

    @Override
    public <T> T selectOne(String statementId, Object param) throws Exception {
        List<Object> objects = selectList(statementId, param);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else if (objects.size() > 1) {
            throw new RuntimeException("查询多个结果...");
        } else {
            return null;
        }
    }

    @Override
    public int update(String statementId, Object param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        return executor.update(configuration, mapperStatement, param);
    }

    @Override
    public int delete(String statementId, Object param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        return executor.delete(configuration, mapperStatement, param);
    }

    @Override
    public int insert(String statementId, Object param) throws Exception {
        MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
        return executor.insert(configuration, mapperStatement, param);
    }

    @Override
    public void close() {
        executor.close();
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //动态代理生成对象
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //执行具体逻辑
                String statementId = method.getDeclaringClass().getName() + "." + method.getName();
                MapperStatement mapperStatement = configuration.getMapperStatementMap().get(statementId);
                SqlCommandTypeEnum sqlCommandType = mapperStatement.getSqlCommandType();
                switch (sqlCommandType) {
                    case SELECT:
                        //查询
                        Type genericReturnType = method.getGenericReturnType();
                        //判断是否带泛型
                        if (genericReturnType instanceof ParameterizedType) {
                            if (args != null) {
                                return selectList(statementId, args[0]);
                            }
                            return selectList(statementId, null);
                        }
                        return selectOne(statementId, args[0]);
                    case UPDATE:
                        //更新
                        return update(statementId, args[0]);
                    case INSERT:
                        //插入
                        return insert(statementId, args[0]);
                    case DELETE:
                        //删除
                        return update(statementId, args[0]);
                }
                return method.invoke(args);
            }
        });
        return (T) proxyInstance;
    }
}
